package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOAPI;
import com.actionsoft.sdk.local.api.ProcessAPI;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class CreateViolationXeportRecord extends ExecuteListener{

	public String getDescription() {
		return "节点审批结束，创建违规通报图片展示卡片。";
	}
	
	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		String kp_prosid = "obj_1b523baf356c405abd28a26ebc3ddb3b";
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
		//获取流程实例ID
		String processInstId = param.getProcessInstance().getId();
		if (flag_ok) {
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_VIOLATIONREPORT  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  datarecord = headlist.get(0);
				Object ID = datarecord.get("ID");// ID
				Object TARGETMAN = datarecord.get("TARGETMAN");// 申请人
				Object TARGETMANPHONE = datarecord.get("TARGETMANPHONE");// 联系电话
				Object VIOLATIONTYPE = datarecord.get("VIOLATIONTYPE");//违规行为
				Object MEMO = datarecord.get("MEMO");// 备注
				String ANNEX = CoreUtil.objToStr(datarecord.get("ANNEX"));// 照片
				BO newcmsrecord = new BO();
				newcmsrecord.set("FID",CoreUtil.objToStr(ID) );
				newcmsrecord.set("TARGETMAN",CoreUtil.objToStr(TARGETMAN));
				newcmsrecord.set("TARGETMANPHONE",CoreUtil.objToStr(TARGETMANPHONE));
				newcmsrecord.set("VIOLATIONTYPE",CoreUtil.objToStr(VIOLATIONTYPE));
				newcmsrecord.set("MEMO",CoreUtil.objToStr(MEMO));
				ProcessAPI proapi = SDK.getProcessAPI();
				// 创建一个仅存储的数据维护实例流程实例ID
				ProcessInstance processinstance = 
						proapi.createBOProcessInstance(kp_prosid, "admin", "违规通报图片展示维护");
				BOAPI boapi = SDK.getBOAPI();
				int emrecordboId =boapi.create("BO_EU_VIOLATIONREPORT_RECORD", newcmsrecord, 
						processinstance, null);	
				if(!"".equals(ANNEX)) {
					String sql = "select id from BO_EU_VIOLATIONREPORT_RECORD t where  t.FID = '"
							+ CoreUtil.objToStr(ID) + "'";
					String sql1 = "select bindid from BO_EU_VIOLATIONREPORT_RECORD t where  t.FID = '"
							+ CoreUtil.objToStr(ID) + "'";
					String cardid = CoreUtil.objToStr(DBSql.getString(sql, "id"));// id
					String bindid = CoreUtil.objToStr(DBSql.getString(sql1, "bindid"));// bindid
					SDK.getBOAPI().copyFileTo(CoreUtil.objToStr(ID), "ANNEX", cardid, "BO_EU_VIOLATIONREPORT_RECORD",
							"ANNEX", bindid, null);
					}
				}
	   }
	
	
}
